package com.xxxx.server.utils;

import org.springframework.stereotype.Component;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class DownLoadUtils {
    public void download(ByteArrayOutputStream byteArrayOutputStream,HttpServletResponse response,String name) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        name = response.encodeURL(new String(name.getBytes(),"UTF-8"));//保存的文件名 必须和编码格式一致
        response.setHeader("content-disposition","attachment;filename=" + name);
        response.setContentLength(byteArrayOutputStream.size());
        ServletOutputStream outputStream = response.getOutputStream();//取得输出流
        byteArrayOutputStream.writeTo(outputStream); //写到输出流
        byteArrayOutputStream.close();//关闭输出流
        outputStream.flush();//刷新数据
    }
}
